//
//  XHomeDropView.m
//  Bees
//
//  Created by Xiaohan on 2018/12/18.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "XHomeDropView.h"


static NSString*const kHomeDropHeadTipIdentifier = @"kHomeDropHeadTipIdentifier";
@interface XHomeDropView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView*tableView;
@end

@implementation XHomeDropView
- (void)setCollectionArr:(NSArray<FoundRecommendModel *> *)collectionArr{
    _collectionArr = collectionArr;
    [self.tableView reloadData];
}
- (void)setRecentlyUseArr:(NSArray<FoundRecommendModel *> *)recentlyUseArr{
    _recentlyUseArr = recentlyUseArr;
    [self.tableView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    self.tableView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.blackColor;
    [self.tableView registerClass:UITableViewHeaderFooterView.class forHeaderFooterViewReuseIdentifier:kHomeDropHeadTipIdentifier];
}

#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XHomeDropCell *cell = [XHomeDropCell cellForTableView:tableView];
    if (indexPath.section==0) {
        cell.datas = self.recentlyUseArr;
    }else{
        cell.datas = self.collectionArr;
    }
    kWeakSelf(self);
    cell.clickItemBlock = ^(FoundRecommendModel * _Nonnull model) {
        !weakself.clickItemBlock?:weakself.clickItemBlock(model);
    };
    return cell;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView*headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHomeDropHeadTipIdentifier];
    headView.contentView.backgroundColor = UIColor.clearColor;
    UIButton*tipBtn = [headView viewWithTag:100033];
    if (!tipBtn) {
        tipBtn = [UIButton new];
        tipBtn.tag = 100033;
        [tipBtn setTitleColor: UIColorHex(#FFCC03) forState:UIControlStateNormal];
        tipBtn.titleLabel.font = kDefaultFont(12);
        tipBtn.frame = CGRectMake(0, 0, 80, 40);
        [tipBtn setImage:IMAGE_NAMED(@"yellow_arrow") forState:UIControlStateNormal];
        [headView addSubview:tipBtn];
    }
    if (section == 0) {
        [tipBtn setTitle:XLocalized(@"最近使用") forState:UIControlStateNormal];
        kWeakSelf(self);
        [tipBtn addTapBlock:^(UIButton * _Nonnull btn) {
            !weakself.HeaderSectionBlock?:weakself.HeaderSectionBlock(XDAPP_Recently);
        }];
    }else{
        kWeakSelf(self);
        [tipBtn setTitle:XLocalized(@"我的收藏") forState:UIControlStateNormal];
        [tipBtn addTapBlock:^(UIButton * _Nonnull btn) {
            !weakself.HeaderSectionBlock?:weakself.HeaderSectionBlock(XDAPP_Collection);
        }];
    }
    
    [tipBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:1];
    return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kHomeDropSectionHeadHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return kHomeDropSectionFooterHeight;
}
#pragma mark - getter
- (UITableView*)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kXHomeDropClvCellHeight;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
